from tortoise.models import Model
from tortoise import fields
from pydantic import BaseModel, Field
from datetime import datetime


# Tortoise-ORM 模型
class Collection(Model):
    collection_id = fields.IntField(pk=True)  # 收藏ID（主键）

    # 多对一关系：多个收藏属于一个用户
    student = fields.ForeignKeyField(
        "models.Student",
        related_name="collections",  # 反向查询名称
        on_delete=fields.CASCADE  # 级联删除
    )

    # 关联收藏的课程ID
    collection_class_id = fields.IntField()  # 存储class_id

    # 收藏时间
    collect_time = fields.DatetimeField(auto_now_add=True)

    # 可选：收藏分类标签
    tag = fields.CharField(max_length=50, null=True)  # 收藏分类标签

    class Meta:
        table = "collections"
        unique_together = (("student", "collection_class_id"),)  # 防止重复收藏


# ---------- Pydantic 模型 ----------


    class Config:
        from_attributes = True